package com.gxuwz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.thymeleaf.spring5.ISpringTemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/WEB-INF/pages/**").addResourceLocations("/WEB-INF/pages/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
    }


    /**
     * 生成模版解析器
     * @return
     */
    @Bean
    public ITemplateResolver templateResolver() {

        // 创建一个SpringResourceTemplateResolver实例
        SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver();
        // 设置模板前缀，指定Thymeleaf查找模板文件的起始位置
        springResourceTemplateResolver.setPrefix("/WEB-INF/pages/");
        // 设置模板后缀，指定模板文件的文件类型
        springResourceTemplateResolver.setSuffix(".html");
        // 设置模板模式，这里使用HTML作为模板模式
        springResourceTemplateResolver.setTemplateMode(TemplateMode.HTML);
        // 设置字符编码，通常使用UTF-8
        springResourceTemplateResolver.setCharacterEncoding("UTF-8");
        springResourceTemplateResolver.setOrder(1);
        springResourceTemplateResolver.setCacheable(false);
        // 返回配置好的模板解析器
        return springResourceTemplateResolver;
    }

    /**
     * 模板解析器注入到模板引擎中
     * @param templateResolver
     * @return
     */

    @Bean
    public ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
        // 创建一个SpringTemplateEngine实例
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        // 设置模板解析器，将之前配置的模板解析器注入到模板引擎中
        springTemplateEngine.setTemplateResolver(templateResolver);
        // 返回配置好的模板引擎
        return springTemplateEngine;
    }

    /**
     * 生成视图解析器并注入模版引擎
     * @param templateEngine
     * @return
     */
    @Bean
    public ViewResolver viewResolver(ISpringTemplateEngine templateEngine) {
        // 创建ThymeleafViewResolver实例
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        // 设置视图解析器的顺序，通常用于指定视图解析器的执行顺序
        thymeleafViewResolver.setOrder(1);
        // 设置字符编码，通常使用UTF-8
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        // 设置模板引擎，将之前配置的模板引擎注入到视图解析器中
        thymeleafViewResolver.setTemplateEngine(templateEngine);
        // 返回配置好的视图解析器
        return thymeleafViewResolver;
    }
}
